/*
 * *
 *  * Created with IntelliJ RustRover.
 *  * Description:
 *  * Copyright Author: ZhenYi
 *  * DateTime: 2024/2/15 下午6:40
 *
 */


use salvo::prelude::*;
use sea_orm::*;
use serde::{Deserialize, Serialize};
use crate::config::pgsql::{maunlist, userlist};


#[derive(Deserialize,Serialize)]
pub struct MaunFindInput {
    pub tp:String,
    pub value:String
}
#[handler]
pub async fn find_maun(req: &mut Request, depot: &mut Depot, res: &mut Response)  {
    let db = depot.obtain::<DatabaseConnection>().unwrap();
    let input = match req.parse_json::<MaunFindInput>().await{
        Ok(input) => input,
        Err(_e) => {
            res.status_code(StatusCode::IM_USED).render("{\"Error\":\"Json parse error\"}");
            return;
        }
    };
    let tp = match input.tp.parse::<i32>(){
        Ok(odk) => odk,
        Err(_) => {
           res.status_code(StatusCode::IM_USED).render("{\"Error\":\"TP should be a number\"}");
            return;
        }
    };
    let value = input.value.clone();
    if tp == 1 {
        let ns = maunlist::Entity::find()
            .filter(maunlist::Column::Maunid.contains(value))
            .all(db )
            .await
            .unwrap();
         res.status_code(StatusCode::ACCEPTED).render(
           serde_json::to_string(&ns).unwrap());
        return;
    }else if tp == 2 {
        let ns = maunlist::Entity::find()
            .filter(maunlist::Column::Title.contains(value))
            .all(db )
            .await
            .unwrap();
       res.status_code(StatusCode::ACCEPTED).render(
           serde_json::to_string(&ns).unwrap());
        return;
    }else if tp == 3 {
        let ns = maunlist::Entity::find()
            .filter(maunlist::Column::Key.contains(value))
            .all(db )
            .await
            .unwrap();
         res.status_code(StatusCode::ACCEPTED).render(
           serde_json::to_string(&ns).unwrap());
        return;
    }else if tp == 4 {
        let ns = maunlist::Entity::find()
            .filter(maunlist::Column::Author.contains(value))
            .all(db )
            .await
            .unwrap();
         res.status_code(StatusCode::ACCEPTED).render(
           serde_json::to_string(&ns).unwrap());
        return;
    }else if tp == 5 {
        let nst = userlist::Entity::find()
            .filter(userlist::Column::Companyname.contains(value))
            .all(db )
            .await
            .unwrap();
        let ns = maunlist::Entity::find()
            .all(db )
            .await
            .unwrap();
        let mut re = Vec::new();
        for i in ns{
            for j in &nst{
                if i.author == j.name{
                    re.push(i.clone());
                }
            }
        }
        res.status_code(StatusCode::ACCEPTED).render(
            serde_json::to_string(&re).unwrap());
        return;
    }else{
        res.status_code(StatusCode::IM_USED).render("{\"Error\":\"TP should be a number between 1 and 5\"}");
        return;
    }
}